package com.dang.alex.servlet.home;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dang.alex.model.Book;
import com.dang.alex.model.Category;
import com.dang.alex.model.Product;
import com.dang.alex.service.imp.ProductServiceImp;
import com.dang.alex.service.imp.ServiceDAOFactory;


@WebServlet({ "/MainServlet", "/main.do" })
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method=req.getParameter("method");
		Map<String,List> datas=new HashMap<String,List>();
		if(method!=null){
			switch(method){
			case "1":
				productDetail(req,resp);
				break;
			default:
				break;
			}
		}
		else{
			//分类浏览
			initCategory(datas);
            //编辑推荐
			initRecommend(datas);
			//热销图书
			initHotBooks(datas);
			//最新上架图书
			initNewBooks(datas);
			
			req.setAttribute("datas", datas);
			req.getRequestDispatcher("/WEB-INF/view/home/main.jsp").forward(req, resp);
		}
	}

/**
 * 最新上架图书
 * @param datas
 */
	private void initNewBooks(Map<String, List> datas) {
		List<Book> books=new ArrayList<Book>();
		try {
			for(Product p : ServiceDAOFactory.getProductServiceImp().findByNew(4)){
				books.add((Book) p);
			}
			datas.put("newBooks", books);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

/**
 * 热销图书
 * @param datas
 */
	private void initHotBooks(Map<String, List> datas) {
		List<Book> books=new ArrayList<Book>();
		List<Product> pros;
		try {
			pros=ServiceDAOFactory.getProductServiceImp().findByHot(4);
			for (Product p : pros) {
				books.add((Book) p);
			}
			datas.put("hotBooks", books);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

/**
 * 编辑推荐
 * @param datas
 */
	private void initRecommend(Map<String, List> datas) {
		List<Book> books=new ArrayList<Book>();
		ProductServiceImp productService=ServiceDAOFactory.getProductServiceImp();
		try {
			/*books.add((Book) productService.findById(new Random().nextInt(20)));
			books.add((Book) productService.findById(new Random().nextInt(20)));*/
			int r=(int)(Math.random()*20)+1;
			int h=(int)(Math.random()*20)+1;
			
			books.add(productService.findById(r));
			books.add(productService.findById(h));
			datas.put("books", books);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

/**
 * 分类浏览
 * @param datas
 */
	private void initCategory(Map<String, List> datas) {
		List<Category> cats;
		try {
			List<Category> list=ServiceDAOFactory.getCategoryService().findAll();
			cats=findByParentId(list,1);
			//給予目錄suCats賦值
			for (Category c : cats) {
				c.setSubCats(findByParentId(list, c.getId()));
			}
			datas.put("category", cats);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private List<Category> findByParentId(List<Category> list, int id) {
		List<Category> subCats=new ArrayList<Category>();
		for (Category c : list) {
			if(c.getParentId()==id){
				subCats.add(c);
			}
		}
	return subCats;
}


	private void productDetail(HttpServletRequest req, HttpServletResponse resp) {
		Book book=null;
		try {
			String id=req.getParameter("id");
			if(id!=null){
				book=(Book)ServiceDAOFactory.getProductServiceImp().findById(new Integer(id));
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		req.setAttribute("book", book);
		try {
			req.getRequestDispatcher("/WEB-INF/view/home/product.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
	}

}
